package com.byedu.jupiter.controller.course.library;

import com.byedu.jupiter.domain.course.QuestionDO;
import com.byedu.jupiter.service.course.QuestionService;
import com.byedu.jupiter.util.enums.CharsetType;
import com.byedu.jupiter.util.enums.FileType;
import com.byedu.jupiter.util.ZipUtil;
import com.byedu.jupiter.util.parsers.library.LibraryExcelParser;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jason on 2017/6/3.
 */
@Controller
@RequestMapping("library")
public class LibraryController {

    private final String BASE_FILEPATH_UNZIP = "E:\\uploads\\unzips\\";
    private final LibraryExcelParser libraryExcelParser = new LibraryExcelParser();

    @Autowired
    private QuestionService questionService;

    @RequestMapping(value = "/upload", method = {RequestMethod.GET})
    public String upload(ModelMap model) {
        return "course/library/libraryupload";
    }

    @RequestMapping(value = "uploadFile", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    @ResponseBody
    public String uploadFile(@RequestParam("multipartFile") CommonsMultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = multipartFile.getOriginalFilename();
        String fileType = fileName.split("[.]")[1];
        DiskFileItem diskFileItem = (DiskFileItem) multipartFile.getFileItem();
        File file = diskFileItem.getStoreLocation();
        List<QuestionDO> questionDOList = null;
        if (FileType.ZIP.getValue().equals(fileType)) {
            questionDOList = getZipFileDatas(file, BASE_FILEPATH_UNZIP, CharsetType.GBK.getValue(), fileName);
        } else if (FileType.XLS.getValue().equals(fileType) || FileType.XLSX.getValue().equals(fileType)) {
            //解析excel文件
//            questionDOList = libraryExcelParser.parseFile(file);
        }
        //文件数据入库


        return "";
    }

    private List<QuestionDO> getZipFileDatas(File file, String distPath, String charset, String fileName) throws Exception {
        //1.解压文件到指定的目录下
        ZipUtil.unzip(file, distPath, charset);
        //2.读取该目录下的文件
        File dir = new File(BASE_FILEPATH_UNZIP + "\\" + fileName + "\\");
        if (dir != null && dir.exists() && dir.isDirectory()) {
            File[] fileAry = dir.listFiles();
            if (fileAry != null && fileAry.length > 0) {
                List<QuestionDO> questionDOList = new ArrayList<QuestionDO>();
                for (File innerFile : fileAry) {
                    //3.解析相应文件
                    if (innerFile.exists() && innerFile.isFile()) {
//                        List<QuestionDO> tmpList = libraryExcelParser.parseFile(innerFile);
//                        questionDOList.addAll(tmpList);
                    }
                }
            }
        }

        return null;
    }
}